package cn.easybuy.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.easybuy.dao.BaseDao;
import cn.easybuy.dao.CarDao;
import cn.easybuy.entity.Car;

public class CarDaoImpl  extends BaseDao implements CarDao{

	@Override
	public int saveCar(Car car) {
		String sql="insert into easybuy_car(`name`,`property`,`number`,`totalprice`) values(?,?,?,?)";
		int num=this.executeUpdate(sql,car.getName(),car.getProperty(),car.getNumber(),car.getTotalprice());
		return num;
	}

	@Override
	public List<Car> getAllCars(String name) {
		String  sql="select * from easybuy_car where `name`=?";
		ResultSet rs =this.exxcuteQuery(sql, name);
		List<Car> list =new ArrayList<Car>();
		try {
			while (rs.next()) {
				Car car =new Car();
				car.setId(rs.getInt(1));
				car.setName(rs.getString(2));
				car.setProperty(rs.getString(3));
				car.setNumber(rs.getInt(4));
				car.setTotalprice(rs.getDouble(5));
				list.add(car);
				
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(connection, pstmt, rs);
		}
		return list;
	}

	@Override
	public int updateCar(Car car) {
		String sql="update easybuy_car set  number=?,totalprice=? where `name`=?";
		int num=this.executeUpdate(sql,car.getNumber(),car.getTotalprice(),car.getName());
		return num;
	}

	@Override
	public int deleteCar(String name) {
		String sql="delete from easybuy_car where name=?";
		int num =this.executeUpdate(sql,name);
		return num;
	}

	@Override
	public List<Car> getAllCarProduct() {
		String sql="select * from easybuy_car";
		ResultSet rs=this.exxcuteQuery(sql);
		List<Car> list =new ArrayList<Car>();
		try {
			while (rs.next()) {
				Car car =new Car();
				car.setId(rs.getInt(1));
				car.setName(rs.getString(2));
				car.setProperty(rs.getString(3));
				car.setNumber(rs.getInt(4));
				car.setTotalprice(rs.getDouble(5));
				list.add(car);
				
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.closeAll(connection, pstmt, rs);
		}
		return list;
	}

}
